package com.example.admin.controller;

import com.example.admin.service.BlogService;
import com.example.xo.common.Result;
import com.example.xo.pojo.Blog;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YangJiaHao
 */
@RestController
@Slf4j
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("")
    public Result add(@RequestBody Blog blog){
        //不能为空 trim：去除两端空格，中间的去不掉  即：null、“”、“    ”
        if (blog.getTitle()==null || blog.getTitle().trim().length()==0){
            log.info("标题{}为空",blog.getTitle());
            return Result.error();
        }
        if (blog.getTitle().length()>50){
            log.info("标题{}长度超过50个字",blog.getTitle());
            return Result.error();
        }
        if (blog.getAuthor().length()>10){
            log.info("作者名{}长度超过10个字",blog.getAuthor());
            return Result.error();
        }
        if (blog.getCategoryId()==null || blog.getCategoryId().trim().length()==0){
           log.info("分类数据丢失");
            return Result.error();
        }
        if (blog.getAuthor()==null || blog.getAuthor().trim().length()==0){
           log.info("作者数据丢失");
            return Result.error();
        }
        if (blog.getContent()==null){
           log.info("博客内容丢失");
            return Result.error();
        }

        boolean result = blogService.add(blog);
        if (result==false){
            return Result.error();
        }
        log.info("是否新增成功：{}",result);
        return Result.success(result);
    }

    @GetMapping("findAll")
    public Result findAll(){
        List<BlogVO> list = blogService.findAll();
        log.info("查询全部博客结果:{}",list);
        return Result.success(list);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable String id){
        return Result.success(blogService.findPoById(id));
    }


    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id){
        boolean result = blogService.delete(id);
        log.info("是否成功删除：{}",result);
        return Result.success(result);
    }

    @PutMapping
    public Result update(@RequestBody Blog blog){
        //不能为空 trim：去除两端空格，中间的去不掉  即：null、“”、“    ”
        if (blog.getTitle()==null || blog.getTitle().trim().length()==0) {
            log.info("标题{}为空", blog.getTitle());
            return Result.error();
        }
        if (blog.getTitle().length()>50){
            log.info("标题长度超过50个字");
            return Result.error();
        }
        if (blog.getCategoryId()==null || blog.getCategoryId().trim().length()==0){
            log.info("分类数据丢失");
            return Result.error();
        }
        if (blog.getContent()==null || blog.getContent().trim().length()==0){
            log.info("博客内容丢失");
            return Result.error();
        }

        boolean result = blogService.update(blog);
        log.info("是否修改成功:{}",result);
        return Result.success(result);
    }
}
